<template>
	<!-- 选择时间 -->
	<u-datetime-picker :visibleItemCount='8' ref="datetimePicker" @confirm="fnConfirm($event)" :minDate="minDate" :maxDate="maxDate" @cancel="fnCloseTimePicker"
		:formatter="formatter" :show="show" v-model="value1" :mode="mode">
		
	</u-datetime-picker>
</template>

<script>
	export default {
		props: {
			mode: {
				default: 'date',
				type: String //year-month
			},
			show: {
				default: false,
				type: Boolean
			}, //显示
			defaultValue: {
				default: '',
				type: String
			},
			timeFormat: {
				default: 'yyyy-mm-dd hh:MM:ss',
				type: String
			},
			minDate: {
				default: 0,
				type: Number
			},
		},
		data() {
			return {
				value1: new Date().getTime(),
				maxDate: Number(new Date()),
			}
		},
		onReady() {
			// 微信小程序需要用此写法
			this.$refs.datetimePicker.setFormatter(this.formatter)
		},
		watch: {
			defaultValue(val) {
				console.log('defaultValue', val)
				// this.$refs.datetimePicker.innerIndex = [0]
				this.$refs.datetimePicker.innerValue = val;
				this.value1 = val;
				this.$forceUpdate()
			}
		},
		// computed: {
		// 	value1() {
		// 		if (this.defaultValue) {
		// 			return new Date(this.defaultValue.replaceAll("-", '/')).getTime()
		// 		} else {
		// 			return new Date().getTime()
		// 		}
		// 	}
		// },
		methods: {
			fnCloseTimePicker() {
				this.$emit('update:show', false)
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			fnConfirm(e) {
				let date = uni.$u.timeFormat(e.value, this.timeFormat);
				this.$emit('confirm', {
					value: e.value,
					date: date
				})

				// let date = uni.$u.timeFormat(e.value, 'yyyy-mm-dd');
				// if (index == 0) {
				// 	this.fnCloseTimePicker()
				// } else {
				// 	if (e.value > Number(new Date(this.startTime))) {
				// 		this.$emit('update:endTime', date)
				// 		this.fnCloseTimePicker()
				// 	} else {
				// 		this.$Msg.Toast('结束时间必须大于开始时间')
				// 	}
				// }
			}
		}
	}
</script>
